package com.dgut.legalservice.controller;

import com.dgut.legalservice.domain.Msg;
import com.dgut.legalservice.domain.User;
import com.dgut.legalservice.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserServiceImpl userServiceiml;

    @RequestMapping("/login")
    public String  login(){

        return "/login/login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "/login/login";
    }
    @RequestMapping("/checkLogin")
    public String checkLogin(HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user==null){
            return "/login/login";
        }else {
            if (user.getType().equals("普通用户"))
                return "/user/header";
            else if (user.getType().equals("超级管理员")||user.getType().equals("管理员"))
                return "/platform/header";
            else
                return "/provider/header";
        }
    }

    @RequestMapping("/loginSubmit")
    public String  loginSubmit(String loginName,HttpSession session){
        User user= userServiceiml.findByLoginName(loginName);  //密码在checkPassword检验，这里根据用户类型进行跳转
        session.setAttribute("user",user);
        if (user.getType().equals("普通用户"))
            return "/user/header";
        else if (user.getType().equals("超级管理员")||user.getType().equals("管理员"))
            return "/platform/header";
        else
            return "/provider/header";
    }

    @RequestMapping("/register")
    public String register(){
      return "/login/register";
    }

    @RequestMapping("/registerSubmit")
    public String registerSubmit(HttpSession session,String loginName,String password,String type,String email){
        User user = new User();
        User user1 = (User) session.getAttribute("user");
        if(user1==null){    //防止反复刷新执行这个方法而反复添加用户
            user.setLoginName(loginName);
            String birth = "2020-12-11";
            LocalDate localDate = LocalDate.parse(birth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            user.setBirthday(localDate);
            user.setPassword(password);
            user.setType(type);
            user.setEmail(email);
            user.setPhotoAttId("../img/头像.png");
            userServiceiml.insertUser(user);  //数据库添加用户
            session.setAttribute("user",user);  //session添加用户属性，让拦截器放行和加入用户信息
        }else{  //反复刷新更新数据
            user = userServiceiml.findByLoginName(user1.getLoginName());  //根据登录名查找出user
            session.setAttribute("user",user);  //更新session的数据
        }
        if (user.getType().equals("普通用户"))
            return "/user/header";
        else if (user.getType().equals("超级管理员")||user.getType().equals("管理员"))
            return "/platform/header";
        else
            return "/provider/header";

    }

    @ResponseBody
    @RequestMapping(value = "/checkEmail")
    public Msg checkEmail(String email) {
        // 根据邮箱查询是否存在该邮箱
        Msg msg = new Msg();
        User user = userServiceiml.findByEmail(email);
        // 当对象不为空，说明用户名存在
        if (user!= null) {
            msg.setCode(200);  //200-用户存在
        }else {
            msg.setCode(100);  //用户不存在
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/checkLoginName")
    public Msg checkLoginName(String loginName) {
        // 根据用户名查询是否存在该用户
        Msg msg = new Msg();
        User user = userServiceiml.findByLoginName(loginName);
        // 当对象不为空，说明用户名存在
        if (user!= null) {
            msg.setCode(200);  //200-用户存在
        }else {
            msg.setCode(100);  //用户不存在
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/checkPassword")
    public Msg checkPassword(HttpSession session,String loginName,String password) {
        // 验证登录密码
        Msg msg = new Msg();
        User user = userServiceiml.findByLoginName(loginName);
        // 取出该用户名账号，取出密码与用输入的密码比对
        if (user!=null){
            if(user.getStatus().equals("停用")){  //账号冻结，不能用
                msg.setCode(400);
            }else {  //账号正常
                if (user.getPassword().equals(password)) {
                    msg.setCode(200);  //200-密码正确
                    session.setAttribute("user",user);
                }else {
                    msg.setCode(100);  //用户密码不正确
                }
            }
        }else {
            msg.setCode(0);   //用户不存在
        }
        return msg;
    }



    @ResponseBody
    @RequestMapping(value = "/checkVcode")
    public Msg getSession(HttpSession session,String loginName,String vCode) {
        // 根据atrribute获取相应的值
        Msg msg = new Msg();
        if (session.getAttribute("vCode")==null){
            msg.setCode(0);
            return msg;
        }
        String checkVcode = (String) session.getAttribute("vCode");
        String checkName = (String) session.getAttribute("checkName");
        if (loginName.equals(checkName)&&vCode.equals(checkVcode)){
            msg.setCode(200);
        }else {
            msg.setCode(100);
        }
        return msg;
    }


    @RequestMapping("/retrieve")
    public String retrieve(){
        return "/login/retrieve";
    }

    @ResponseBody
    @RequestMapping(value = "/getQuestion")
    public String getQuestion(String loginName) {
        String question = userServiceiml.findQuestion(loginName);
        return question;
    }

    @ResponseBody
    @RequestMapping(value = "/checkAnswer")
    public Msg checkAnser(String loginName,String answer) {
        Msg msg = new Msg();
        String answer1 = userServiceiml.findAnswer(loginName);
        if (answer1.equals(answer)){
            msg.setCode(200);
        }else
            msg.setCode(100);
        return msg;
    }

    @RequestMapping("/resetPassword")
    public String reset(HttpSession session,String loginName,String password){
        User user = userServiceiml.findByLoginName(loginName);
        user.setPassword(password);
        userServiceiml.updatePassword(user);
        session.setAttribute("user",user);
        if (user.getType().equals("普通用户"))
            return "/user/header";
        else if (user.getType().equals("超级管理员")||user.getType().equals("管理员"))
            return "/platform/header";
        else
            return "/provider/header";
    }
    @RequestMapping("/user-info")
    public String userInfo(HttpSession session){  //传值session
        User user = (User) session.getAttribute("user");
        if (user.getType().equals("普通用户"))
            return "/user/user-info";
        else if (user.getType().equals("超级管理员")||user.getType().equals("管理员"))
            return "/platform/user-info";
        else
            return "/provider/user-info";
    }
    @RequestMapping("/company-info")
    public String companyInfo(HttpSession session){  //传值session
        return "/provider/company-info";
    }

    @RequestMapping("/password-update")
    public String passwordUpdate(HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user.getType().equals("普通用户")){
            return "/user/password-update";
        }else if (user.getType().equals("超级管理员")||user.getType().equals("管理员")){
            return "/platform/password-update";
        }else {
            return "/provider/password-update";
        }
    }


    @RequestMapping("/header")
    public String header(HttpSession session){  //传值session
        User user = (User) session.getAttribute("user");
        if (user.getType().equals("普通用户"))
            return "/user/header";
        else if (user.getType().equals("超级管理员")||user.getType().equals("管理员"))
            return "/platform/header";
        else
            return "/provider/header";
    }

    @RequestMapping("/user-list")
    public String userList(HttpSession session){
        return "/platform/findNotSuper";
    }
}
